<?PHP

$password = $_POST['password'];
$admin_email = $_POST['email'];


$school = ['哈尔滨工业大学（深圳）', '华南理工大学', '华中科技大学', '华中师范大学', '暨南大学', '南方科技大学', '武汉大学', '湘潭大学', '中南大学', '中山大学'];
$site_arr = array
    (
    array(5, 6, 7, 1, 2, 3, 8, 9, 10, 4),
    array(4, 5, 6, 2, 3, 1, 9, 10, 8, 7),
    array(5, 6, 4, 3, 1, 2, 7, 8, 9, 10),
    array(6, 4, 5, 10, 1, 2, 8, 9, 7, 3),
    array(3, 4, 5, 1, 2, 10, 9, 7, 8, 6),
    array(4, 5, 3, 2, 10, 1, 6, 7, 8, 9),
    array(5, 3, 4, 9, 10, 1, 7, 8, 6, 2),
    array(2, 3, 4, 10, 1, 9, 8, 6, 7, 5),
    array(3, 4, 2, 1, 9, 10, 6, 7, 5, 8),
    array(4, 2, 3, 10, 8, 9, 7, 5, 6, 1)
);

$judge_arr_A = array
    (
    array('1a', '1a', '2a', '3a', '9b', '10a'),
    array('10a', '1a', '2a', '8b', '9b', '10a'),
    array('10a', '1a', '2a', '8b', '9b', '10a'),
    array('10a', '1a', '2a', '8b', '9b', '10a'),
    array('9a', '1a', '7b', '8b', '9a', '10b'),
    array('9a', '1a', '7b', '8b', '9a', '10b'),
    array('9a', '1a', '7b', '8b', '9a', '10b'),
    array('8a', '6a', '7b', '8a', '9a', '10b'),
    array('8a', '6a', '7b', '8a', '9a', '10b'),
    array('8a', '6a', '7b', '8a', '9a', '10b')
);

$judge_arr_B = array
    (
    array('7a', '5a', '6a', '7a', '8a', '9a'),
    array('7a', '5a', '6a', '7a', '8a', '9a'),
    array('7a', '5a', '6a', '7a', '8a', '9a'),
    array('6a', '4b', '5a', '6a', '7a', '8a'),
    array('6a', '4b', '5a', '6a', '7a', '8a'),
    array('6a', '4b', '5a', '6a', '7a', '8a'),
    array('5a', '3b', '4b', '5a', '6b', '7a'),
    array('5a', '3b', '4b', '5a', '6b', '7a'),
    array('5a', '3b', '4b', '5a', '6b', '7a'),
    array('4a', '2b', '3b', '4a', '5b', '6b')
);

$judge_arr_C = array
    (
    array('4a', '2b', '3b', '4a', '5b', '6b'),
    array('4a', '2b', '3b', '4a', '5b', '6b'),
    array('3a', '1b', '2b', '3a', '4a', '5b'),
    array('3a', '1b', '2b', '3a', '4a', '5b'),
    array('3a', '1b', '2b', '3a', '4a', '5b'),
    array('2a', '1b', '2a', '3a', '4a', '10a'),
    array('2a', '1b', '2a', '3a', '4a', '10a'),
    array('2a', '1b', '2a', '3a', '4a', '10a'),
    array('1a', '1a', '2a', '3a', '9b', '10a'),
    array('1a', '1a', '2a', '3a', '9b', '10a')
);

for ($i = 1; $i <= 10; $i++) {
    if (!empty($_POST["school_number_$i"])) {
        $school_number[$i] = $_POST["school_number_$i"];
    } else {
        $school_number[$i] = '';
    }
}

$mysqli = mysqli_init();

$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2); //设置超时时间
$mysqli->real_connect('127.0.0.1', 'root', 'mysql');
$sql = "CREATE DATABASE IF NOT EXISTS CUPT DEFAULT CHARSET utf8 COLLATE utf8_general_ci;";
if ($mysqli->query($sql) == TRUE) {
    //printf("Table CUPT successfully created.\n");
}
mysqli_select_db($mysqli, "CUPT");
//建表
/*$sql = "CREATE TABLE IF NOT EXISTS `rounds_match_list` (
        `site` varchar(3) DEFAULT NULL,
        `round` varchar(3) DEFAULT NULL,
        `session` varchar(3) DEFAULT NULL,
        `school_1_number` varchar(20) DEFAULT NULL,
        `school_2_number` varchar(20) DEFAULT NULL,
        `school_3_number` varchar(20) DEFAULT NULL,
        `chairman_number` varchar(10) DEFAULT NULL,
        `judge_1_number` varchar(10) DEFAULT NULL,
        `judge_2_number` varchar(10) DEFAULT NULL,
        `judge_3_number` varchar(10) DEFAULT NULL,
        `judge_4_number` varchar(10) DEFAULT NULL,
        `judge_5_number` varchar(10) DEFAULT NULL,
        `admin_email` varchar(20) DEFAULT NULL,
         UNIQUE key (site,round,session)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

if (mysqli_query($mysqli, $sql)) {
    //echo "数据表 participant 创建成功";
} else {
    //echo "创建数据表错误: " . mysqli_error($mysqli);
}*/







for ($session = 1, $string4 = ''; $session <= 10; $session++) {
    if ($session == 1) {
        $round = 1;
        $string4 = $string4 . "<th rowspan='4' style='width:100px'>第" . "$round" . "轮</th>";
    } elseif ($session == 5) {
        $round = 2;
        $string4 = $string4 . "<th rowspan='3' style='width:100px'>第" . "$round" . "轮</th>";
    } elseif ($session == 8) {
        $round = 3;
        $string4 = $string4 . "<th rowspan='3' style='width:100px'>第" . "$round" . "轮</th>";
    }
    $site_arr_tmp=$site_arr[$session-1];
    for ($site = 1; $site <= 3; $site++) {
        if ($site == 1) {
            $judge_tmp = $judge_arr_A[$session - 1];
            
        } elseif ($site == 2) {
            $judge_tmp = $judge_arr_B[$session - 1];
        } elseif ($site == 3) {
            $judge_tmp = $judge_arr_C[$session - 1];
        }
                $site_tmp_1 = ($site - 1) * 3 + 0;
                $site_tmp_2 = ($site - 1) * 3 + 1;
                $site_tmp_3 = ($site - 1) * 3 + 2;
                $sql = "INSERT INTO rounds_match_list(site,round,session,school_1_number,school_2_number,school_3_number,chairman_number,judge_1_number,judge_2_number,judge_3_number,judge_4_number,judge_5_number,admin_email) VALUES('$site','$round','$session','$site_arr_tmp[$site_tmp_1]','$site_arr_tmp[$site_tmp_2]','$site_arr_tmp[$site_tmp_3]','$judge_tmp[0]','$judge_tmp[1]','$judge_tmp[2]','$judge_tmp[3]','$judge_tmp[4]','$judge_tmp[5]','$admin_email');";
                //echo "<br/>" . $sql;
                $mysqli->query($sql);
                
        
        //
    }
    $string4 = "<tr>" . $string4 . "</tr>";
}


/*for ($judge = 1, $string = ''; $judge <= 5; $judge++) {
    for ($score = 1; $score <= 3; $score++) {
        $string = $string . "`judge_" . "$judge" . "_score_" . "$score" . "` varchar(3) DEFAULT NULL,";
    }
}

$sql = "CREATE TABLE IF NOT EXISTS `session_match_list` (
        `site` varchar(3) DEFAULT NULL,
        `round` varchar(3) DEFAULT NULL,
        `session` varchar(3) DEFAULT NULL,
        `accept_question` varchar(3) DEFAULT NULL,
        `refuse_question` varchar(50) DEFAULT NULL,
        $string
        `admin_email` varchar(20) DEFAULT NULL,
        UNIQUE KEY (site,round,session)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

if (mysqli_query($mysqli, $sql)) {
    //echo "数据表 participant 创建成功";
} else {
    //echo "创建数据表错误: " . mysqli_error($mysqli);
}*/


$sql = "select * from volunteer where `email`='$admin_email' and `password`='$password';";
$rst = $mysqli->query($sql);
while ($row = mysqli_fetch_assoc($rst)) {
    $secret_key = $row['secret_key'];
}
if ($rst->num_rows < 1) {
    echo '<br />用户名或密码错误！<br />';
    echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到信息录入界面</a>';
} elseif ($secret_key == NULL) {
    echo '<br />您没有管理员权限！<br />';
    echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到信息录入界面</a>';
} else {
    //echo '<br />登录成功';
    //if ($flag == 0) {//抽签结果录入或修改
    for ($i = 1; $i <= 10; $i++) {
        $j = $i - 1;
        $sql = "INSERT INTO school_information_list(school_number,school) VALUES('$school_number[$i]','$school[$j]');";
        $mysqli->query($sql);
        $sql = "UPDATE school_information_list SET school_number='$school_number[$i]' WHERE school = '$school[$j]';";
        $mysqli->query($sql);
        $sql = "UPDATE judge SET school_number='$school_number[$i]' WHERE school = '$school[$j]';";
        $mysqli->query($sql);
    }
    echo "<br/>数据录入成功";
    mysqli_close($mysqli);
}
?>
